package me.yushuai.wukong.base.service.impl;

import me.yushuai.wukong.base.service.LotDefService;
import me.yushuai.wukong.base.api.dto.CreateLotDef;
import me.yushuai.wukong.base.convertor.LotDefConvertor;
import me.yushuai.wukong.base.entity.LotAttrDef;
import me.yushuai.wukong.base.entity.LotDef;
import me.yushuai.wukong.base.repository.LotAttrDefRepository;
import me.yushuai.wukong.base.repository.LotDefRepository;
import me.yushuai.wukong.common.exceptin.DuplicateBizIdException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 批次定义业务类
 *
 * @author zhoushuai@189.cn
 * @since 2023-03-01
 */
public class LotDefServiceImpl implements LotDefService {

    @Autowired
    private LotDefRepository lotDefRepository;

    @Autowired
    private LotAttrDefRepository lotAttrDefRepository;

    @Autowired
    private LotDefConvertor lotDefConvertor ;

    @Override
    public void createLotDef(CreateLotDef def) throws DuplicateBizIdException {
        final LotDef lotDef = lotDefConvertor.mapToLotDef(def);
        this.lotDefRepository.save(lotDef);

        List<LotAttrDef> lotAttrDefs = this.lotDefConvertor.mapToLotAttrDefList(def.getLotAttrList());
        lotAttrDefs = lotAttrDefs.stream().peek(s -> s.setId(lotDef.getId())).collect(Collectors.toList());
        this.lotAttrDefRepository.saveAll(lotAttrDefs);
    }
}
